<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.utilities.date');

class iJoomla_SurveysModelLinks extends JModel{
	
	function getLink(){
		$database =& JFactory::getDBO();
		global $mainframe;
		$s_id = JRequest::getVar("id", "0");
		
		$sql = "SELECT alias FROM #__ijoomla_surveys_surveys WHERE id=".$s_id;
		$database->setQuery($sql);
		$survey_title = $database->loadResult() ;
		if($database->getErrorNum() ) {
			echo $database->stderr() ;
			return false ;
		}
		
		$sql = "select id from #__menu where link='index.php?option=com_surveys'";
		$database->setQuery($sql);
		$database->query();
		$result = $database->loadResult();
		
		if($result != NULL){
			$homelink = "index.php?option=com_surveys&Itemid=".$database->loadResult();
			$link = $homelink."&survey=".$s_id.":".$survey_title;	
			$link = $this->buildRoute($link);					
		}else{
			$link = $mainframe->getSiteURL();
		}
		return $link;
	}
	
	function buildRoute($link){
		return $link;
		/*global $mainframe;
		$database=&JFactory::getDBO();
		$sef			= $mainframe->getCfg("sef");
		$sef_rewrite	= $mainframe->getCfg("sef_rewrite");
		$sef_suffix		= $mainframe->getCfg("sef_suffix");
		//sef is not set
		if($sef==0){
			return $link;
		}	
		else{
			$link=explode("index.php?",$link);
			$array_link=explode("&",$link[1]);
			$sef_link="";
			
			//start sh404
			$sql="select enabled from #__components where `option`='com_sh404sef'";
			$database->setQuery($sql);
			$database->query();
			if($database->getNumRows()>0){
				$result=$database->loadObject();
				if ($result->enabled==1){
					require_once(JPATH_SITE.DS."administrator".DS."components".DS."com_sh404sef".DS."config".DS."config.sef.php");
					if($Enabled==1){
						for($i=0;$i<count($array_link);$i++){
							$parts=explode("=",$array_link[$i]);
							if($parts[0]=="act"){
								$sef_link.=$parts[1]."/";
							}
							else if($parts[0]=="survey"){
								$val=explode(":",$parts[1]);
								$sef_link.=$val[1]."/";
							}
						}
						if($shRewriteMode!=0) $sef_link="index.php/".$sef_link;
						if (trim($suffix)!="") $sef_link=substr($sef_link,0,-1).".".$suffix;
						return JURI::root().$sef_link;
					}
				}
			}
			//end sh404
			
			//start sef advance
			$sql="select enabled from #__components where `option`='com_sef'";
			$database->setQuery($sql);
			$database->query();
			if($database->getNumRows()>0){
				$result=$database->loadObject();
				if ($result->enabled==1){
					$sql="select * from #__sef_config";
					$database->setQuery($sql);
					$database->query();
					$result=$database->loadObject();
					if($result->enabled==1){
						$custom_comp=$result->custom_comp;
						$array_custom_comp=explode(",",$custom_comp);
						$custom="surveys";
						for($i=0;$i<count($array_custom_comp);$i++){
							$parts=explode("=>",$array_custom_comp[$i]);
							if($parts[0]=="surveys") $custom=$parts[1];
						}
						for($i=0;$i<count($array_link);$i++){
							$parts=explode("=",$array_link[$i]);
							if($parts[0]=="option"){
								$sef_link.=$custom."/";
							}
							else if ($parts[0]=="Itemid"){
								if($result->alias==0){
									$sql="select name from #__menu where id=".intval($parts[1]);
									$database->setQuery($sql);
									$database->query();
									$val=$database->loadObject();
									$val=str_replace($result->space,"%11",$val->name);
									$val=str_replace(" ",$result->space,$val);
									$sef_link.=$val."/";	
								}
								else{
									$sql="select alias from #__menu where id=".intval($parts[1]);
									$database->setQuery($sql);
									$database->query();
									$val=$database->loadObject();
									$sef_link.=$val->alias."/";	
								}
							}
							else if($parts[0]=="survey"){
								$val=explode(":",$parts[1]);
								if($result->alias==0){
									$sql="select title from #__ijoomla_surveys_surveys where s_id=".intval($val[0]);
									$database->setQuery($sql);
									$database->query();
									$val=$database->loadObject();
									$val=str_replace($result->space,"%11",$val->title);
									$val=str_replace(" ",$result->space,$val);
									if($result->lowercase==1) $sef_link.=strtolower($val)."/";
									else $sef_link.=$val."/";
								}
								else $sef_link.=$val[1]."/";
							}
						}
						if(trim($result->sufix)!="" && $sef_suffix==1)
							$sef_link=substr($sef_link,0,-1).$result->sufix;
						if($sef_rewrite==0) $sef_link="index.php/".$sef_link;
						return JURI::root().$sef_link;
					}
				}
			}
			//end sef advance
			
			//start default sef
			for($i=0;$i<count($array_link);$i++){
				$parts=explode("=",$array_link[$i]);
				if($parts[0]=="Itemid"){
						$sql="select alias from #__menu where id=".intval($parts[1]);
						$database->setQuery($sql);
						$database->query();
						$val=$database->loadObject();
						$sef_link.=$val->alias."/";
				}
				else if($parts[0]=="survey"){
					$val=explode(":",$parts[1]);
					$sef_link.=$val[0]."-".$val[1]."/";
				}
			}
			if($sef_rewrite==0) $sef_link="index.php/".$sef_link;
			return JURI::root().$sef_link;
			//end default sef
		}*/		
	}
}

?>

